home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / OTPAPSampleServer / SetServerStatusOption.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  2.8 KB  |  95 lines  |  [TEXT/CWIE]

  1. #include <Memory.h>
  2. #include "OpenTransport.h"            // open transport files            
  3. #include "OpenTptAppleTalk.h"
  4.  
  5. /*
  6. **    prototype declaration
  7. */
  8. extern OSStatus DoSetServerStatusOption(EndpointRef ep, char *statusStr);
  9.  
  10.  
  11. /*
  12.     global variables
  13.     The nature of the following call is such that you'll want to set the option 
  14.     when the endpoint is in asynch mode.  To handle the asynch case, you must
  15.     declare the global gOptionCompleted.  When an option management call is made
  16.     gOptionCompleted is set to zero.  When the T_OPTMGMTCOMPLETE call is handled
  17.     by the endpoint handler, then gOptionCompleted is set to 1.  Note that the
  18.     sample spin loops until gOptionCompleted is non-zero.  As such, the 
  19.     DoSetServerStatusOption call cannot be made at interrupt time or from a deferred
  20.     task.  The DoSetServerStatusOption can only be made at system task time.
  21.     
  22.     Ensure that gOptionCompleted is used to flag only one outstanding Option 
  23.     Management call at a time.
  24.     
  25. */
  26. extern UInt32    gOptionCompleted;
  27.  
  28. /*
  29.     input parameters
  30.     ep - endpoint on which to set the status string option
  31.     statusstr - C style string of the status string to set
  32.     
  33.     function result - kOTNoError indicates that the option was successfully negotiated
  34.         if the result is negative, then this is the result returned by the call to
  35.         OTOptionManagement.  If the result is positive, then this is the value of 
  36.         the status field on return from the call to 
  37.  
  38. */
  39. OSStatus DoSetServerStatusOption(EndpointRef ep, char *statusStr)
  40.  
  41. {
  42.     UInt8        buf[kOTOptionHeaderSize+256];    // define buffer to allow for 
  43.                                                 // up to a 256 char string
  44.     TOption*    opt;                            // option ptr to make items easier to access
  45.     TOptMgmt    req;
  46.     TOptMgmt    ret;
  47.     UInt32         statusStrLen;
  48.     OSStatus    err;
  49.     Boolean        isAsync = false;
  50.     
  51.     if (OTIsSynchronous(ep) == false)            // check whether ep sync or not
  52.     {
  53.         isAsync = true;
  54.         gOptionCompleted = 0;
  55.     }
  56.     
  57.     statusStrLen = OTStrLength(statusStr);    // get the length of the C string        
  58.     opt = (TOption*)buf;                    // set option ptr to buffer
  59.     req.opt.buf    = buf;
  60.     req.opt.len    = kOTOptionHeaderSize + statusStrLen;
  61.     req.flags    = T_NEGOTIATE;                // negotiate for serverstatus option
  62.  
  63.     ret.opt.buf = buf;
  64.     ret.opt.maxlen = kOTOptionHeaderSize+256;
  65.     
  66.  
  67.     opt->level    = ATK_PAP;                    // dealing with tpi
  68.     opt->name    = OPT_SERVERSTATUS;
  69.     opt->len    = kOTOptionHeaderSize + statusStrLen;
  70.     opt->status = 0;
  71.         // set the serverStr into the value field
  72.     BlockMove(statusStr, (Ptr)&opt->value, statusStrLen);
  73.  
  74.     err = OTOptionManagement(ep, &req, &ret);
  75.     
  76.     if ((isAsync == true) && (err == kOTNoError))
  77.     {
  78.         while (gOptionCompleted == 0)
  79.         {
  80.             // spin in this null loop waiting for the option call to complete.
  81.         }
  82.     }
  83.     
  84.         // if no error then return the option status value
  85.     if (err == kOTNoError)
  86.     {
  87.         if (opt->status != T_SUCCESS)
  88.             err = opt->status;
  89.         else
  90.             err = kOTNoError;
  91.     }
  92.         
  93.     return err;
  94. }
  95.